import "../base.js"
import {AjaxError, errorHandler, AJAXERROR} from "../Error"

// action type
export const GETCOURSE = "GETCOURSE"
export const SETCOURSE = "SETCOURSE"
export const DELCOURSE = "DELCOURSE"
exports.AJAXERROR = AJAXERROR

export const REFRESH = "REFRESH"
export const SORT = "SORT"
export const SEARCH = "SEARCH"
export const FILTER = "FILTER"
export const CHANGEPAGE = "CHANGEPAGE"
export const SELECTCOURSE = "SELECTCOURSE"
export const MOVE = "MOVE"
export const ADDCOURSE = "ADDCOURSE"
export const DELITEM = "DELITEM"

// action functions
export function getCourse(courseId) {
    return fetch(`/education/course/specific/${courseId}`)
        .then(response => response.json())
        .then(json => {
            if (json.status !== 200) {
                throw new AjaxError(json, -1);
            }
            return {
                type: GETCOURSE,
                course: json.msg.course
            }
        }).catch(errorHandler)
}

export function setCourse(course) {
    return fetch(`/education/admin/course/info/edit`, {
        method: "POST",
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({
            title: course.title,
            description: course.description,
            cover: course.cover,
            location: course.location,
            major: course.major,
            id: course.id
        })
    })
        .then(res => res.json())
        .then(json => {
            if(json.error){
                return {
                    type : AJAXERROR
                }
            }
            return {
                type: SETCOURSE,
                course: json.msg
            }
        }).catch(errorHandler)
}

export function delCourse(id,videoLocation){
    return fetch(`/cleanVideo`,{        // 先清理视频文件
        method : "POST",
        headers: {'Content-Type': 'application/json'},
        body : JSON.stringify({
            videoLocation
        })
    })
        .then(res => res.json())
        .then(json => {
            if(json.isDeleted = true){

                return fetch(`/education/admin/course/del`,{
                    method : "POST",
                    headers: {'Content-Type': 'application/json'},
                    body : JSON.stringify({
                        id
                    })
                })
                    .then(res => res.json())
                    .then(json => {
                        return {
                            type : DELCOURSE
                        }
                    })

            }else{

                return {
                    type : AJAXERROR
                }

            }
        }).catch(errorHandler)

}

// common action functions
export function getCourseList(courseId) {
    return fetch(`/education/admin/course/files/all?courseId=${courseId}`)
        .then(response => response.json())
        .then(json => {
            // console.log(json.msg)
            if(json.status !== 200){
                throw new AjaxError(json,-1);
            }
            return {
                type : REFRESH,
                courses : json.msg
            }
        }).catch(errorHandler)
}

export function sortCourseList(sortIdx,sortFlag){
    return {
        type : SORT,
        sortIdx : sortIdx,
        sortFlag : sortFlag
    }
}

export function searchCourse(searchText){
    return {
        type : SEARCH,
        searchText : searchText
    }
}

/**
 * 根据类别筛选课程
 * @param filterName
 * @returns {{type: string, filterName: *}}
 */
export function filterCourse(filterName) {
    return {
        type : FILTER,
        filterName : filterName
    }
}

/**
 * 改变页数或者每页个数
 * @param page
 * @param limit
 */
export function changePage(page,limit) {
    return {
        type : CHANGEPAGE,
        page : page,
        limit : limit
    }
}

/**
 * 选中课程
 * @param course
 */
export function selectCourse(course){
    return {
        type : SELECTCOURSE,
        course : course,
    }
}

/**
 * 上移或者下移
 * @param direction
 * @param course
 * @returns {{type: string}}
 */
// todo : 只有一个条目的时候会报错
export function move(direction,course){
    return {
        type : MOVE,
        direction : direction,
        course : course
    }
}

/**
 * 上传文件
 */
export function addCourse(item){
    return fetch(`/education/admin/course/files/add`, {
        method: "POST",
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({
            title: item.title,
            location: item.location,
            courseId : item.courseId
        })
    })
        .then(res => res.json())
        .then(json => {
            return {
                type : ADDCOURSE,
                course : json.msg
            }
        }).catch(errorHandler)
}

/**
 * 删除附件
 * @param id
 */
export function delFile(id){
    return fetch(`/education/admin/course/files/del`,{
        method: "POST",
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({
            id
        })
    })
        .then(res => res.json())
        .then(json => {
            toastr.success("删除成功")
            return {
                type : DELITEM,
                id
            }
        }).catch(errorHandler)
}

/**
 * 评论列表
 * @param courseId
 */
export function getCommentsList(courseId){
    return fetch(`/education/course/comments/all?courseId=${courseId}&isNote=0`)
        .then(response => response.json())
        .then(json => {
            if (json.status !== 200) {
                throw new AjaxError(json, -1);
            }

            return {
                type: REFRESH,
                courses : json.msg
            }
        }).catch(errorHandler)
}

/**
 * 删除评论
 * @param id
 */
export function delComment(id){
    return fetch(`/education/admin/course/comments/del`,{
        method: "POST",
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({
            id,
            isNote : "0"
        })
    })
        .then(res => res.json())
        .then(json => {
            toastr.success("删除成功")
            return {
                type : DELITEM,
                id
            }
        }).catch(errorHandler)
}

/**
 * 得到笔记列表
 * @param courseId
 */
export function getNotesList(courseId){
    return fetch(`/education/course/comments/all?courseId=${courseId}&isNote=1`)
        .then(response => response.json())
        .then(json => {
            if (json.status !== 200) {
                throw new AjaxError(json, -1);
            }

            return {
                type: REFRESH,
                courses : json.msg
            }
        }).catch(errorHandler)
}

/**
 * 删除笔记
 * @param id
 */
export function delNote(id){
    return fetch(`/education/admin/course/comments/del`,{
        method: "POST",
        headers: {'Content-Type': 'application/json'},
        body: JSON.stringify({
            id,
            isNote : 1
        })
    })
        .then(res => res.json())
        .then(json => {
            toastr.success("删除成功")
            return {
                type : DELITEM,
                id
            }
        }).catch(errorHandler)
}